package com.atguigu.gmall.order.service.impl;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.common.util.HttpClientUtil;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.order.service.OrderService;
import com.atguigu.gmall.user.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/10/21 15:02
 * BMW F750GS
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private  RedisTemplate redisTemplate;


    @Override
    public Map trade(HttpServletRequest request) {
        HashMap<String, Object> result = new HashMap<>();
        //获取用书收件人信息
        List<UserAddress> userAddressList = userFeignClient.getUserAddressList();
        result.put("userAddressList",userAddressList);
        //获取商品清单
        List<CartInfo> cartInfoListByChecked = cartFeignClient.getCartInfoListByChecked();
        //页面需要OrderDetail类型,
        List<OrderDetail> orderDetailList = cartInfoListByChecked.stream().map(cartInfo -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setSkuId(cartInfo.getSkuId());
            orderDetail.setImgUrl(cartInfo.getImgUrl());
            orderDetail.setSkuName(cartInfo.getSkuName());
            orderDetail.setSkuNum(cartInfo.getSkuNum());
            orderDetail.setOrderPrice(cartInfo.getSkuPrice());
            return orderDetail;
        }).collect(Collectors.toList());
        result.put("detailArrayList",orderDetailList);
        // 小计:商品数量，总商品金额
        //商品总数量
        IntSummaryStatistics collect = orderDetailList.stream().collect(Collectors.summarizingInt(OrderDetail::getSkuNum));
        long totalNum = collect.getSum();
        result.put("totalNum",totalNum);
        //总金额   totalPrice =  totalPrice + 单价*数量
        BigDecimal totalAmount = new BigDecimal("0");
        for (OrderDetail orderDetail : orderDetailList) {
            totalAmount =totalAmount.add(orderDetail.getOrderPrice().multiply(new BigDecimal(orderDetail.getSkuNum())));
        }
        result.put("totalAmount",totalAmount);
        
        //订单号、交易号 使用UUid
        String uuid = UUID.randomUUID().toString();
        String userId = AuthContextHolder.getUserId(request);
        //将订单交易号保存到redis缓存中
        redisTemplate.opsForValue().set("tradeNo:"+userId,uuid);
        result.put("tradeNo",uuid);

        return result;
    }
    /**
     * 检验库存
     * */
    @Override
    public boolean hasStock(OrderDetail orderDetail) {
        //返回1有库存,0为没有库存
        String s = HttpClientUtil.doGet("http://www.gware.com:9001/hasStock?skuId=" +
                orderDetail.getSkuId() + "&num=" + orderDetail.getSkuNum());
        return "1".equals(s);
    }
}
